/**
 * AI人物类
 */
class AIUserMgr {
	private static instance: AIUserMgr;
	public static getInstance(): AIUserMgr {
		if (this.instance == null) {
			this.instance = new AIUserMgr();
		}
		return this.instance;
	}

	public constructor() {
	}

	public userList = new Array();
	public userOnline = 0;
	public c = serverConfig.config;

	public currentPlayer;


	/**创建一个玩家 */
	public createPlayer(tuserId, name) {
		//var currentPlayer=new userBase();
		var currentPlayer = {
			troops: 1,
			/**杀敌数 */
			killNumber: 0,
			/**遮罩时间 */
			zhetime: 0,
			name: name,
			userId: tuserId,
			roomId: -1,
			x: 0,
			y: 0,
			frogType: Math.floor(Math.random() * 2 + 0),
			level: 1,
			type: "player",
			lastHeartbeat: Date.now(),
			lineLength: AIPropsMgr.getInstance().GetAttributesByGrade(0).frogLineLength,
			tongueWide: AIPropsMgr.getInstance().GetAttributesByGrade(0).tongueWide,
			grade: 0,
			speed: this.c.initUserSpeed,
			radius: 5,
			//表示玩家是否为AI
			isAI: false,
			//是否跟玩家连接
			ishadLine: false,
			//伙伴
			partner: -1,
			disconnection_line: false,
			disconnection_line_time: -1,
			/**是否吃到道具*/
			propStates: [{
				getPropType: -1,
				PropStartTime: -1,
			}, {
				getPropType: -1,
				PropStartTime: -1,
			}, {
				getPropType: -1,
				PropStartTime: -1,
			}, {
				getPropType: -1,
				PropStartTime: -1,
			}, {
				getPropType: -1,
				PropStartTime: -1,
			}, {
				getPropType: -1,
				PropStartTime: -1,
			}, {
				getPropType: -1,
				PropStartTime: -1,
			}, {
				getPropType: -1,
				PropStartTime: -1,
			},
			{
				getPropType: -1,
				PropStartTime: -1,
			}],

			//用于游戏房间来判断玩家是否死亡
			death:
			{
				isDeath: false,
				deadStartTime: 0
			},
			//判断玩家是否处于新手保护
			NewHandProtect:
			{
				isProtect: false,
				ProtectStartTime: 0

			},
			target: {
				x: 0,
				y: 0
			},
			targetdir: {
				x: 0,
				y: 0
			},
			dressup: {
				decorateId: -1,
				trailingId: -1
			},

			shootStates:
			{
				shootStartTime: 0,
				isShoot: false,
				point: {
					x: 0,
					y: 0
				}
			},
			size: 50,
			otherLineUser: -1,
			//0 normal,1 fly ,2 stun ,3 ice
			states: 0,
		};
		if (DataProl.tuanduizhan) {
			currentPlayer.frogType = currentPlayer.troops//团队战自己是黄色
		}
		//console.log(tuserId + "创建成功...");
		this.userList[tuserId] = currentPlayer;
		this.currentPlayer = currentPlayer;
		this.userOnline++;
	};


	public getAllUser() {
		return this.userList;
	}

	public addAI(user) {
		this.userList[user.userId] = user;
	};

	public del(userId) {
		delete this.userList[userId];
		this.userOnline--;
	};

	public get(userId) {
		return this.userList[userId];
	};

	public isOnline(userId) {
		var data = this.userList[userId];
		if (data != null) {
			return true;
		}
		return false;
	};

	public getOnlineCount() {
		return this.userOnline;
	}

	public kickOutPlayer(player) {
		var userList = AIServer.getInstance().roomInfo.roomUser;
		for (var t = 0; t < userList.length; t++) {
			if (userList[t].userId == player.userId) {
				userList.splice(t, 1);
				console.log("删除一个ai....");
			}
		}
	}

	/**初始化玩家们 */
	public restUsers() {
		this.cleanRoomAis();
	}



	//将玩家从房间内提出
	public kickUser(user) {
		if (user == null)
			return;
		if (user.isAI)
			return; this.del(user.userId);
		//this.kickOutPlayer(user);
		// if(socket!=null)
		//   socket.close()

	};
	private cleanRoomAis() {
		this.currentPlayer.killNumber = 0;
		var mm = 100;
		for (var i = 0; i < this.userList.length; ++i) {
			if (this.userList[i] != null && this.userList[i] != undefined) {
				this.resetPlayer(this.userList[i], mm);
			}
		}
		AIServer.getInstance().roomInfo.roomUser = [];
	}
	/**给玩家设定一个保护罩 */
	public setProtectShiled(player) {
		player.NewHandProtect.isProtect = true;
		player.NewHandProtect.ProtectStartTime = Date.now();
	}
	/**用于重置玩家所有道具状态 */
	public resetPlayerPropStates = function (player) {
		if (player == null) {
			console.log("重置玩家道具为空");
			return;
		}
		player.speed = this.c.initUserSpeed;
		player.NewHandProtect.isProtect = false;
		player.NewHandProtect.ProtectStartTime = 0;
		for (var i = 0; i < player.propStates.length; i++) {
			player.propStates[i].getPropType = 0;
			player.propStates[i].PropStartTime = 0;
		}

	}
	/**用于重置玩家单个道具状态 */
	public resetPlayerOnePropStates = function (player, i) {
		if (player == null) {
			console.log("重置玩家道具为空");
			return;
		}
		if (i == 1 || i == 7) {
			player.speed = this.c.initUserSpeed;
		}
		// player.NewHandProtect.isProtect = false;
		// player.NewHandProtect.ProtectStartTime = 0;
		player.propStates[i].getPropType = 0;
		player.propStates[i].PropStartTime = 0;
		// for (var i = 0; i < player.propStates.length; i++) {
		// 	player.propStates[i].getPropType = 0;
		// 	player.propStates[i].PropStartTime = 0;
		// }

	}
	/**
	 * 用于重置玩家的状态
	 * @param {} roomId 
	 */
	public resetPlayer(player, mm?: number) {//mm控制青蛙击杀是否清零，只有退房间时击杀清零

		if (player == null) {
			console.log("重置玩家为空");
			return;
		}

		if (player.death != null) {
			player.death.isDeath = false;
			player.death.deadStartTime = null;
		}
		//      console.log("回收"+player.name);
		if (mm == 100) {
			player.killNumber = 0;
		}
		if (!DataProl.isXinShou) {
			player.grade = 0;
		}
		player.NewHandProtect.isProtect = false;
		player.NewHandProtect.ProtectStartTime = null;
		player.lineLength = AIPropsMgr.getInstance().GetAttributesByGrade(0).frogLineLength;
		player.tongueWide = AIPropsMgr.getInstance().GetAttributesByGrade(0).tongueWide
		player.ishadLine = false;
		//团队战青蛙死亡不变色
		if (DataProl.tuanduizhan) {
			player.frogType = player.troops
		} else {
			player.frogType = Math.floor(Math.random() * 2 + 0);
			if (DataProl.isXinShou) {
				player.frogType = 2;
			}
		}
		player.partner = null;
		player.otherLineUser = -1;
		player.disconnection_line = false;
		player.disconnection_line_time = 1;
		for (var i = 0; i < player.propStates.length; i++) {
			player.propStates[i].getPropType = 0;
			player.propStates[i].PropStartTime = 0;
		}
		player.dressup.decorateId = -1;
		player.dressup.trailingId = -1;
		player.speed = this.c.initUserSpeed;
		player.states = 0;
		player.x = player.y = 0;
		player.targetdir.x = player.targetdir.y = 0;
		if(player.userId!=DataProl.account){
		player.name = randomName.getInstance().getRandomName();
		}
	}
}